home *** CD-ROM | disk | FTP | other *** search
- Path: news.sover.net!news
- From: sstryker@sover.net (Stew Stryker)
- Newsgroups: comp.lang.c++
- Subject: Newbie Question on using function ptr or Casting?
- Date: Thu, 14 Mar 1996 13:02:44 -0400
- Organization: Southern Vermont Network
- Message-ID: <19960314170244.sstryker@sover.net>
- NNTP-Posting-Host: pm0a1.wrj.sover.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-NewsReader: Emissary News v1.01.007, by Wollongong Inc.
-
- [WARNING: C++ NEWBIE ALERT]
-
- I'm working through some code a peer wrote, trying to use it for a new
- application. In his code, he's used a technique that he calls casting that
- I'm not familiar with.
-
- He has a base class which implements a linked list. One of its methods is
- called get_next_record(), which returns the pointer to the next record
- (hence, the name...).
-
- In the derived class, which holds a list of employees, he's printing out
- all the values in the list in a print() function (very good naming of
- methods, eh? :^))
-
- The print function looks like this:
-
- void employees::print() {
-
- employees *current = this;
-
- while (current) {
- cout "Employee name: " << current->emp_name << "\n";
-
- current = (employees*)current->get_next_record();
- }
-
- cout << "End of employee list.\n";
- }
-
- My confusion is on the line that reads:
-
- current = (employees*)current->get_next_record();
-
- He says that he's casting the results of the linked_list get_next_record()
- function from a void pointer (which is how this base class method is
- defined) to a pointer of the employees type. In my reading of my only C++
- manual (Teach Yourself C++ Programming in 21 Days, by Jesse Liberty), this
- looks kinda like a function pointer. This book also refers briefly to
- casting of data, and says it's generally a sign of a poor design.
-
- So, what is going on here? Is there a better way to retrieve the pointer
- to the next record from the base function? Can you try to put you answer
- in simple terms that a newbie could understand?
-
- Thanks,
-
- Stew
-
- For those of you who really like this stuff (I'm not there yet), I've
- included the header for the linked list class:
-
- class linked_list {
-
- private:
- void* back_pointer;
- linked_list *next,
- *prev;
-
- public:
- Boolean last;
- Boolean first;
-
- int status_code;
-
- linked_list(char*, void*); // Constructor.
- linked_list( linked_list*, char*, void*); // Constructor.
- ~linked_list(); // Destructor.
-
- virtual void fetch_row();
- // Virtual function to print out the record.
- virtual void print();
- // True virtual to return the address of the derived class.
- // Function to access the next record.
- void* get_next_record();
- void* get_prev_record();
- void remove_from_list();
- void add_to_list(linked_list*);
- };
-